\defmodule {DiscreteDistIntChart}

This class provides tools to plot the mass function and the cumulative
probability of a discrete probability distribution over the integers.


\bigskip\hrule
\begin{code}
\begin{hide}
/*
 * Class:        DiscreteDistIntChart
 * Description:  
 * Environment:  Java
 * Software:     SSJ 
 * Copyright (C) 2001  Pierre L'Ecuyer and Universite de Montreal
 * Organization: DIRO, Universite de Montreal
 * @author       Richard Simard
 * @since        May 2008

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   <a href="http://www.gnu.org/licenses">GPL licence site</a>.
 */
\end{hide}
package umontreal.iro.lecuyer.charts;
   import umontreal.iro.lecuyer.probdist.DiscreteDistributionInt;\begin{hide}

import org.jfree.chart.JFreeChart;
import javax.swing.JFrame;
import java.awt.*;
\end{hide}


public class DiscreteDistIntChart \begin{hide} {
   protected DiscreteDistributionInt dist;
   protected int a,b;
   protected XYLineChart cdfChart;
   protected XYLineChart probChart;
\end{hide}\end{code}

%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsubsection*{Constructors}
\begin{code}

   public DiscreteDistIntChart (DiscreteDistributionInt dist) \begin{hide} {
      this.dist = dist;
      this.a = 0;
      this.b = 0;
   }\end{hide}
\end{code}
\begin{tabb}
 Constructor for a new \texttt{DiscreteDistIntChart} instance used to plot the
probabilities of the
% \externalclass{umontreal.iro.lecuyer.probdist}{DiscreteDistributionInt}
discrete distribution \texttt{dist} over the integers.
\end{tabb}
\begin{htmlonly}
   \param{dist}{discrete distribution to plot}
\end{htmlonly}
\begin{code}

   public DiscreteDistIntChart (DiscreteDistributionInt dist, int a, int b) \begin{hide} {
      this.dist = dist;
      if (a>=b) throw new IllegalArgumentException ("a is bigger than b");
      this.a = a;
      this.b = b;
      init();
   }\end{hide}
\end{code}
\begin{tabb}
 Constructor for a new \texttt{DiscreteDistIntChart} instance used to plot the
probabilities of the
% \externalclass{umontreal.iro.lecuyer.probdist}{DiscreteDistributionInt}
discrete distribution \texttt{dist} over the interval $[a,b]$.
\end{tabb}
\begin{htmlonly}
   \param{dist}{continuous distribution to plot}
   \param{a}{lower bound of interval}
   \param{b}{upper bound of interval}
\end{htmlonly}

%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection*{Methods}

\begin{code}\begin{hide}

   private void init() {
      int m = b-a+1;
      double[][] cdf = new double[2][m];
      double[][] probability = new double[2][m];

      for (int i = 0; i < m; i++) {
         cdf[0][i] = i+a;
         cdf[1][i] = dist.cdf (i+a);
         probability[0][i] = i+a;
         probability[1][i] = dist.prob (i+a);
      }

      double[][] cdfFinal = new double[2][2*(m-1)];
      for (int i = 0; i < m-1; i++) {
         cdfFinal[0][2*i] = cdf[0][i];
         cdfFinal[0][2*i+1] = cdf[0][i+1];
         cdfFinal[1][2*i] = cdf[1][i];
         cdfFinal[1][2*i+1] = cdf[1][i];
      }

      cdfChart = new XYLineChart("cdf: " + dist.toString(), "", "", cdfFinal);
      probChart = new XYLineChart("probability: " + dist.toString(), "",
                  "", probability);
      cdfChart.setprobFlag (true);
      probChart.setprobFlag (true);

      // Only for tikZ
      XYListSeriesCollection collec = cdfChart.getSeriesCollection();
      collec.setColor(0, Color.BLUE);
      collec.setPlotStyle(0, "thick");
      collec.setMarksType(0, "only marks");

      collec = probChart.getSeriesCollection();
      collec.setColor(0, Color.ORANGE);
      collec.setPlotStyle(0, "ycomb");
      collec.setMarksType(0, "*");
      collec.setDashPattern(0, "solid");
   }

   private void testParam() {
      if (a==0 && b==0) {
         double mean = dist.getMean();
         double sd = dist.getStandardDeviation();
         int xa = (int)Math.round(mean - 3.0*sd);
         if (xa < dist.getXinf())
            xa = dist.getXinf();
         int xb = (int)Math.round(mean + 3.0*sd);
         if (xb > dist.getXsup())
            xb = dist.getXsup();
         setParam(xa, xb);
      }
   }
\end{hide}

   public JFrame viewCdf (int width, int height, int a, int b) \begin{hide} {
      setParam(a,b);
      return cdfChart.view(width, height);
   }\end{hide}
\end{code}
\begin{tabb}
   Displays a chart of the cumulative distribution function (cdf) over the
   interval $[a,b]$ on the screen using Swing. This method creates an
   application containing a chart panel displaying the chart. The created
   frame is positioned on-screen, and displayed before it is returned. The
   \texttt{width} and the \texttt{height} of the chart are measured in pixels.
\end{tabb}
\begin{htmlonly}
   \param{width}{frame width in pixels.}
   \param{height}{frame height in pixels.}
   \param{a}{lower bound of interval}
   \param{b}{upper bound of interval}
   \return{frame containing the chart}
\end{htmlonly}
\begin{code}

   public JFrame viewCdf (int width, int height) \begin{hide} {
      testParam();
      return cdfChart.view(width, height);
   }\end{hide}
\end{code}
\begin{tabb}
 Similar to method \method{viewCdf}{} above.
 If the interval $[a,b]$ for the graph is
 not defined, it will be  set automatically to $[\mu - 3\sigma, \mu + 3\sigma]$,
 where $\mu$ and $\sigma$ are the mean and the variance of the distribution.
\end{tabb}
\begin{htmlonly}
   \param{width}{frame width in pixels}
   \param{height}{frame height in pixels}
   \return{frame containing the chart}
\end{htmlonly}
\begin{code}

   public JFrame viewProb (int width, int height, int a, int b) \begin{hide} {
      setParam(a,b);
      return probChart.viewBar(width, height);
   }\end{hide}
\end{code}
\begin{tabb}
   Displays a chart of the probability mass function over the interval $[a,b]$
   on the screen using Swing. This method creates an application containing
   a chart panel displaying the chart. The created frame is positioned
   on-screen, and displayed before it is returned. The \texttt{width} and
   the \texttt{height} of the chart are measured in pixels.
\end{tabb}
\begin{htmlonly}
   \param{width}{frame width in pixels.}
   \param{height}{frame height in pixels.}
   \param{a}{lower bound of interval}
   \param{b}{upper bound of interval}
   \return{frame containing the chart}
\end{htmlonly}
\begin{code}

   public JFrame viewProb (int width, int height) \begin{hide} {
      testParam();
      return probChart.viewBar(width, height);
   }\end{hide}
\end{code}
\begin{tabb}
 Similar to method \method{viewProb}{} above. 
 If the interval $[a,b]$ for the graph is
 not defined, it will be  set automatically to $[\mu - 3\sigma, \mu + 3\sigma]$,
 where $\mu$ and $\sigma$ are the mean and the variance of the distribution.
\end{tabb}
\begin{htmlonly}
   \param{width}{frame width in pixels.}
   \param{height}{frame height in pixels.}
   \return{frame containing the chart}
\end{htmlonly}
\begin{code}

   public void setParam (int a, int b) \begin{hide} {
      if (a >= b) throw new IllegalArgumentException
            ("a is bigger than b" + a + "  " + b);
      this.a = a;
      this.b = b;
      init();
   }\end{hide}
\end{code}
\begin{tabb}
   Sets the parameters $a$ and $b$ for this object.
\end{tabb}
\begin{htmlonly}
   \param{a}{lower bound of interval}
   \param{b}{upper bound of interval}
\end{htmlonly}
\begin{code}

   public String toLatexCdf (int width, int height) \begin{hide} {
      testParam();
      return cdfChart.toLatex(width, height);
   }\end{hide}
\end{code}
\begin{tabb}
   Exports a chart of the cumulative probability to a \LaTeX\ source code using
  PGF/TikZ.
   This method constructs and returns a string that can be written to
   a \LaTeX\ document to render the plot. \texttt{width} and \texttt{height}
   represents the width and the height of the produced chart. These dimensions
   do not take into account the axes and labels extra space. The \texttt{width}
   and the \texttt{height} of the chart are measured in centimeters.
\end{tabb}
\begin{htmlonly}
   \param{width}{Chart's width in centimeters}
   \param{height}{Chart's height in centimeters}
   \return{LaTeX source code}
\end{htmlonly}
\begin{code}

   public String toLatexProb (int width, int height) \begin{hide} {
      testParam();
      return probChart.toLatex(width, height);
   }\end{hide}
\end{code}
\begin{tabb}
   Similar to \method{toLatexCdf}{}, but for the probability instead
   of the cdf.
\end{tabb}
\begin{htmlonly}
   \param{width}{Chart's width in centimeters}
   \param{height}{Chart's height in centimeters}
   \return{LaTeX source code}
\end{htmlonly}
\begin{code}

   public XYLineChart getCdf () \begin{hide} {
      return cdfChart;
   }\end{hide}
\end{code}
\begin{tabb}
   Returns the chart of the cdf.
\end{tabb}
\begin{htmlonly}
   \return{the chart of the cdf.}
\end{htmlonly}
\begin{code}

   public XYLineChart getProb () \begin{hide} {
      return probChart;
   }\end{hide}
\end{code}
\begin{tabb}
   Returns the chart of the probability.
\end{tabb}
\begin{htmlonly}
   \return{the chart of the probability.}
\end{htmlonly}
\begin{code}
\begin{hide}
}\end{hide}
\end{code}
